for


for

The instruction for is used to repeat the execution of one group of instructions a specific number of times.
La instrucción for es usada para repetir la ejecución de un grupo de instrucciones un número específico de veces.

Tip
Typically, the instruction for is used with an integer variable that allows counting the number of repetitions. For instance, if a document is sent to the printer and two copies of it are required, the instruction for can be used to print the document twice.
Típicamente, la instrucción for es usada con una variable entera que permite contar el número de repeticiones. Por ejemplo, si un documento es enviado a la impresora y dos copias del mismo son requeridas, la instrucción for puede ser usada para imprimir el documento dos veces.

The syntax of the instruction for

The figure shown below shows an instruction for to repeat the group of instructions twice. The block for begins by setting the variable i to one. The condition is then evaluated; as the value of i is less than 3, the group of instructions inside the block executes. Then, the value of i is incremented to 2, and the group of instructions executes again. Finally, the value of i is incremented to 3, the condition i<3 returns false and the instruction for ends.
La figura de abajo muestra una instrucción for para repetir un grupo de instrucciones dos veces. El bloque for comienza fijando la variable i en uno. La condición es entonces evaluada; como el valor de i es menor que 3, el grupo de instrucciones dentro del bloque se ejecuta. Entonces, el valor de i se incrementa a 2, y el grupo de instrucciones se vuelve a ejecutar. Finalmente, el valor de i es incrementado a 3, la condición i<3 regresa un valor de falso y la instrucción for termina.

for

Tip
The structure of the instruction for is shown below. As it can be seen from the figure, the instruction for has three parts: initialization, condition and change. The initialization part is executing just once at the beginning of the for. The condition and change parts are executed every time the group of instructions executed. The condition part allows the instruction for to complete under specific conditions. The change part, as its name implies, modifies one or more variables each time the group of instructions gets executed.
La estructura de la instrucción for es mostrada debajo.Como puede verse de la figura, la instrucción for tiene tres partes: inicialización, condicional y cambio. La parte de inicialización se ejecuta una sola vez al principio del for. Las partes de condicional y de cambio se ejecutan cada vez que el grupo de instrucciones se ejecuta. La parte condicional permite a la instrucción for completar bajo condiciones específicas. La parte de cambio, como su nombre lo implica, modifica una o más variables cada vez que el grupo de instrucciones se ejecuta.

ForStructure

Tip
The instruction for can be represented in a block diagram or flow diagram as shown below. When the instruction for starts, the value value of i is set to one. In this case, the condition stops the for when the value of i is not less than three. The change part increments the value of i by one each time.
La instrucción for puede ser representada en un diagrama de bloques o un diagrama de flujo como se muestra debajo. Cuando la instrucción for comienza, el valor de i es fijado en uno. En este caso, la condicional detiene el for cuando el valor de i no es menor a tres. La parte de cambio incrementa el valor de i de uno en uno cada vez.

block_diagram

Tip
In the instruction for the group of instructions may include only one instruction. In this case, the curly brackets are optional as shown in the codes below (all of them are equivalent).
En la instrucción for el grupo de instrucciones puede incluir solamente una instrucción. En este caso, las llaves son opcionales como se muestra en los códigos de abajo (todos ellos son equivalentes).

Program.cpp
//_______________________________ Code A
int i = 0;
int y = 0;
for(i = 1; i < 3; i++)
{
     y += i;
}

//_______________________________ Code B
int i = 0;
int y = 0;
for(i = 1; i < 3; i++) y += i;

//_______________________________ Code C
int i = 0;
int y = 0;
for(i = 1; i < 3; i++)
     y += i;

//_______________________________ Code D
int y = 0;
for(int i = 1; i < 3; i++)
{
     y += i;
}

Tip
The instruction for is typically used with a variable that control the number of times that the group of instructions is executed. When the instruction for completes, the associated variable has a value that makes the condition false. In other words, at the end of the instruction for, the associated variable has always a value out or range. For instance, in the code shown below the value of i when the instruction for completes is three. Observe that inside the for the variable i takes the values of: 0, 1 and 2.
La instrucción for es típicamente usada con una variable que controla el número de veces que el grupo de instrucciones se repite. Cuando la instrucciónfor termina, la variable asociada tiene un valo que hace la condición false. En otras palabras, al final de la instrucción for, la variable asociada siempre tiene un valor fuera de rango. Por ejemplo, en el código mostrado debajo el valor de i cuando la instrucción for termina es de 3. Observe que dentro del for la variable i toma los valores de: 0, 1 y 2.

Program.h
void Program::Window_Open(Win::Event& e)
{
     int y = 0;
     int i = 0;
     for(i = 0; i < 3; i++) // i: 0, 1, 2
     {
          y += i;
     }
     // The value of i is three
}

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home